# @FileName  :  resource.py
# @Time      :  2025/8/25
# @Author    :  Your Name
# @info      :  资源相关的Pydantic模型

from datetime import date, datetime
from typing import Optional, List
from pydantic import BaseModel, Field
from app.schemas.teacher import DictionarySimple




class ResourceBase(BaseModel):
    """资源基础模型"""
    name: str = Field(..., max_length=200, description="资源名称")
    code: str = Field(..., max_length=50, description="资源编码")
    cloud_id: str = Field(..., max_length=100, description="云存储ID")
    file_format: str = Field(..., max_length=20, description="文件格式")
    file_size: str = Field(..., max_length=20, description="文件大小")
    expiry_date: Optional[date] = Field(None, description="到期时间")
    category_id: int = Field(..., description="分类ID")
    teacher_id: Optional[int] = Field(None, description="上传人ID")
    course_id: Optional[int] = Field(None, description="关联课程ID")


class ResourceCreate(ResourceBase):
    """创建资源模型"""
    pass


class ResourceUpdate(BaseModel):
    """更新资源模型"""
    name: Optional[str] = Field(None, max_length=200, description="资源名称")
    code: Optional[str] = Field(None, max_length=50, description="资源编码")
    cloud_id: Optional[str] = Field(None, max_length=100, description="云存储ID")
    file_format: Optional[str] = Field(None, max_length=20, description="文件格式")
    file_size: Optional[str] = Field(None, max_length=20, description="文件大小")
    expiry_date: Optional[date] = Field(None, description="到期时间")
    category_id: Optional[int] = Field(None, description="分类ID")
    teacher_id: Optional[int] = Field(None, description="上传人ID")
    course_id: Optional[int] = Field(None, description="关联课程ID")


class ResourceResponse(ResourceBase):
    """资源响应模型"""
    id: int
    download_count: int = Field(default=0, description="下载次数")
    category: DictionarySimple = Field(..., description="分类")
    teacher_name: Optional[str] = Field(None, description="上传人名称")
    course_name: Optional[str] = Field(None, description="课程名称")
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True

    @classmethod
    def from_orm(cls, obj):
        # 从ORM对象创建Pydantic模型实例
        data = obj.__dict__
        if obj.teacher:
            data['teacher_name'] = obj.teacher.name
        if obj.course:
            data['course_name'] = obj.course.title
        return cls(**data)


class ResourceListResponse(BaseModel):
    """资源列表响应模型"""
    total: int
    items: List[ResourceResponse]


class ResourceQuery(BaseModel):
    """资源查询参数"""
    name: Optional[str] = Field(None, description="资源名称模糊查询")
    code: Optional[str] = Field(None, description="资源编码查询")
    category_id: Optional[int] = Field(None, description="分类查询")
    teacher_id: Optional[int] = Field(None, description="上传人查询")
    course_id: Optional[int] = Field(None, description="关联课程查询")
    skip: int = Field(0, ge=0, description="跳过记录数")
    limit: int = Field(10, ge=1, le=100, description="返回记录数")
